﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectAttentionActivity : WebActivity
    {

        public static String Attention(WebContext context, ulong project_id, out bool IsAttention)
        {
            var pm = DataFactory.Instance().ProjectMember(project_id, context.Token.UserId.Value);

            if (pm == null)
            {
                IsAttention = false;
                return "+关注";
            }
            else
            {
                IsAttention = true;
                return "已关注";
            }
        }
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var refer_id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
           {
               return new Web.UITextDialog() { Title = "关注Id" };
           }), 0);

            var project = DataFactory.Instance().Project(refer_id);


            if (project == null)
            {
                this.Prompt("未有此项目");
            }
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Context.Send("Login", true);
            }

            var section = this.AsyncDialog("section", g => this.DialogValue("1"));
            var row = this.AsyncDialog("row", g => this.DialogValue("1"));
            var ui = this.AsyncDialog("UI", g => this.DialogValue("none"));

            var vale = new UMC.Web.WebMeta().Put("section", section).Put("row", row).Put("method", "VALUE").Put("reloadSinle", true);



            var member = DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);
            if (member != null)
            {
                switch (member.AuthType ?? WebAuthType.All)
                {
                    case WebAuthType.Guest:
                        break;
                    case WebAuthType.User:
                        this.AsyncDialog("Confrm", g => new UIConfirmDialog("您是此项目的专栏作家，需要取消关注吗"));
                        break;
                    case WebAuthType.Admin:
                        if ((project.ExpireTime ?? 0) == 0)
                        {
                            this.Prompt("此为个人项目，不能改变项目主");
                        }
                        if (DataFactory.Instance().ProjectMember(project.Id.Value, WebAuthType.Admin).Length == 1)
                        {
                            this.Prompt("你是最后一个项目主，不能退出此项目");
                        }
                        this.AsyncDialog("Confrm", g => new UIConfirmDialog("您是此项目的项目主，需要取消关注吗"));
                        break;
                }

                DataFactory.Instance().Delete(member);
                var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
                DataFactory.Instance().Put(new Attention
                {
                    Id = user.Id,
                    ProjectTotal = (ax.ProjectTotal ?? 0) - 1
                });
                DataFactory.Instance().Put(new Project
                {
                    Id = project.Id,
                    MemberTotal = (project.MemberTotal ?? 0) - 1
                });
                this.Context.UIEvent("UI.Edit", ui, vale.Put("value", new UMC.Web.WebMeta().Put("button", "+关注").Put("button-color", "#e67979")), true);

            }
            else
            {
                DataFactory.Instance().Put(new ProjectMember
                {
                    project_id = refer_id,
                    user_id = user.Id,
                    Alias = user.Alias,
                    CreationTime = DateTime.Now,
                    AuthType = WebAuthType.Guest
                });

                DataFactory.Instance().Put(new ProjectDynamic
                {
                    Time = UMC.Data.Utility.TimeSpan(DateTime.Now),
                    user_id = user.Id,
                    Explain = "关注了项目",
                    project_id = refer_id,
                    Alias = user.Alias,
                    refer_id = 0,
                    Title = "项目成员",
                    Type = DynamicType.Member
                });

                var ax = DataFactory.Instance().Attention(user.Id.Value) ?? new Attention { Id = user.Id };
                DataFactory.Instance().Put(new Attention
                {
                    Id = user.Id,
                    ProjectTotal = (ax.ProjectTotal ?? 0) + 1
                });
                DataFactory.Instance().Put(new Project
                {
                    Id = project.Id,
                    MemberTotal = (project.MemberTotal ?? 0) + 1
                });
                this.Context.UIEvent("UI.Edit", ui, vale.Put("value", new UMC.Web.WebMeta().Put("button", "已关注").Put("button-color", "#25b864")), true);

            }



        }

    }
}